package chat.logic;

import chat.server.Session;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @time Oct 28, 2009, 20:57:17 PM
 * @author fdcube
 * @version FreeChat-Server v1.1.2
 */
public class ClientAction {

    private ClientAction() {
    }

    private static String Login(String UserName, String UserPwd) {
        if (UserPwd.equals("kf")) {
            try {
                if (User.UserNameList.length() > 0) {
                    if (User.UserNameList.getJSONArray("UserName").length() > 0) {
                        for (int i = 0; i < User.UserNameList.getJSONArray("UserName").length(); i++) {
                            if (User.UserNameList.getJSONArray("UserName").getString(i).equals(UserName)) {
                                return "0";
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
                return "2";
            }
            String sid = Session.NewSessionId();
            //User uins = new User(UserName, Session.NewSessionId());
            if (User.AddUser(sid, UserName)) {
                return sid;
            } else {
                return "2"; //Server Error
            }
        } else {
            return "0"; //Login Failed
        }
    }

    private static String SendMsg(String UserName, String Msg, String Color) {
        Message msgins = new Message(UserName, Msg, Color);
        if (Message.AddMessage(msgins)) {
            return "1"; //OK
            } else {
            return "2"; //Server Error
            }
    }

    private static String GetMsg(int MsgCount) {
        return Message.getMessages(MsgCount);
    }

    private static String GetUserOnline(String OlUid) {
        return User.GetUsers(OlUid);
    }

    private static String LogOut(String SessionId) {
        if (User.RemoveUser(SessionId)) {
            return "1"; //OK
            } else {
            return "2"; //Server Error
            }
    }

    private static Boolean CheckMsg(JSONObject Jmsg) {
        try {
            if (Jmsg.getString("MsgType").equals("i") && Jmsg.has("SessionId")) {
                System.out.println("No Login");
                return false;
            } else if (!Jmsg.getString("MsgType").equals("i") && User.GetUserName(Jmsg.getString("SessionId")) == null) {
                System.out.println("No this people");
                return false;
            } else {
                System.out.println("MsgType OK!");
                return true;
            }
        } catch (JSONException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public static String Act(String ReqMsg) {
        try {
            JSONObject Jmsg = new JSONObject(ReqMsg);
            if (CheckMsg(Jmsg)) {
                char MsgType = Jmsg.getString("MsgType").charAt(0);
                switch (MsgType) {
                    case 'i':
                        return Login(Jmsg.getString("UserName"), Jmsg.getString("UserPwd"));
                    case 'o':
                        return LogOut(Jmsg.getString("SessionId"));
                    case 's':
                        return SendMsg(User.GetUserName(Jmsg.getString("SessionId")), Jmsg.getString("MsgContent"), Jmsg.getString("MsgColor"));
                    case 'g':
                        return GetMsg(Integer.parseInt(Jmsg.getString("MsgId")));
                    case 'u':
                        return GetUserOnline(Jmsg.getString("OlUid"));
                    default:
                        return "0";
                }
            } else {
                return "0";
            }
        } catch (JSONException ex) {
            Logger.getLogger(ClientAction.class.getName()).log(Level.SEVERE, null, ex);
            return "0";
        }
    }
}
